package com.neu.fenli2023623.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neu.fenli2023623.dto.DoctorDto;
import com.neu.fenli2023623.entity.Doctor;
import com.neu.fenli2023623.service.DoctorService;
import com.neu.fenli2023623.util.ResultVo;
import com.neu.fenli2023623.vo.DoctorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/doctor")
public class DoctorController {

    @Autowired
    private DoctorService doctorService;

    @RequestMapping("queryall")
    public ResultVo queryalldoctor(@RequestBody DoctorDto doctorDto){
        PageHelper.startPage(doctorDto.getCurrentPage(),doctorDto.getPageSize(),true);
        List<DoctorVo> users = doctorService.selectall();
        PageInfo<DoctorVo> pageInfo = new PageInfo<>(users);
        return ResultVo.ok().data("list",users).data("total",pageInfo.getTotal());
    }//显示所有医生的信息

    @RequestMapping("adddoctor")
    public ResultVo adddDoctor(@RequestBody DoctorDto doctorDto){
        int i = doctorService.addDoctor(doctorDto);
        if(i>0){
            return ResultVo.ok("添加成功");
        }else {
            return ResultVo.error("添加失败");
        }
    }//添加医生信息

    @DeleteMapping("{id}")
    public ResultVo deleteDoctor(@PathVariable("id") Long id) {
        int i = doctorService.deleteDoctorById(id);
        if (i > 0) {
            return ResultVo.ok("删除成功");
        } else {
            return ResultVo.error("删除失败");
        }
    }//}删除医生信息操作

    @PostMapping("{name}")
    public ResultVo selectByName(@PathVariable("name") String name){
        List<Doctor> doctors = doctorService.selectDoctorByName(name);
        if(doctors!=null){
            return ResultVo.ok().data("doctorlist",doctors);
        }else{
            return ResultVo.error("查询失败,无该医生");
        }
    }//模糊查询医生信息

    @GetMapping("{id}")
    public ResultVo queryById(@PathVariable("id") Long id){
        Doctor doctor = doctorService.selectByPrimaryKey(id);
        return ResultVo.ok().data("doctor",doctor);
    }

    @RequestMapping("updateDoctor")
    public ResultVo update(@RequestBody DoctorDto doctorDto){
        int i = doctorService.updateDoctor(doctorDto);
        if (i>0) {
            return ResultVo.ok("更新成功");
        }else {
            return ResultVo.error("更新失败");
        }
    }//更新医生信息


}
